home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bchelp10.zip / TI418.ASC < prev    next >
Text File  |  1991-09-11  |  23KB  |  859 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  TURBO C                               NUMBER : 418
  9.   VERSION  :  1.0/1.5
  10.        OS  :  MS-DOS
  11.      DATE  :  February 23, 1988                     PAGE  : 1/14
  12.  
  13.     TITLE  :  EXTENDED FILE I/O LIBRARY
  14.  
  15.  
  16.  
  17.  
  18.   The following is public domain information that has been uploaded
  19.   to  our Forum on CompuServe.  As a courtesy to our users that  do
  20.   not  have  immediate  access  to  CompuServe,  Technical  Support
  21.   distributes these routines free of charge.
  22.  
  23.   However,  because these routines are public domain programs,  not
  24.   developed by Borland International,  we are unable to provide any
  25.   technical support or assistance using these routines. If you need
  26.   assistance   using   these   routines,    or   are   experiencing
  27.   difficulties,  we  recommend  that you log  onto  CompuServe  and
  28.   request  assistance  from the Forum members that developed  these
  29.   routines.
  30.  
  31.   Written by
  32.     Randy Forgaard,  CompuServe 70307,521
  33.  
  34.   Translated to Turbo C by
  35.     Charles Jazdzewski
  36.  
  37.   Many   thanks   to  Bela  Lubkin, (CompuServe   76703,3015)   for
  38.   masterminding   this   idea,   and  Kim   Kokkonen,   (CompuServe
  39.   72457,2131)  for  helping me debug it.   For more  discussion  of
  40.   Handle Tables and the implementation of DOS  redirection,  please
  41.   see  Stan Mitchell,  "Command Line Redirection," PC Tech Journal,
  42.   January 1986, Page 44.
  43.  
  44.   Due to a limitation of DOS,  Turbo  C, version 1.5 only allows up
  45.   to 15 files at a time  to  be  open.  The following module allows
  46.   you  to have up to 96 files open simultaneously under DOS 2.0  or
  47.   2.1, or 252 files open simultaneously under DOS  3.0  or greater.
  48.   This module, in its current form, CANNOT be used with streams nor
  49.   networks.
  50.  
  51.   To use this module edit your CONFIG.SYS file (see the  DOS manual
  52.   for details), so that it includes a line  that  says "FILES=XXX".
  53.   XXX should be a version that is 3 greater than the maximum number
  54.   files  you  wish  to  open,  and  should  not  exceed 99 (for DOS
  55.   2.0/2.1) or 255 (for DOS 3.0 and higher). Under  any  version  of
  56.   DOS, the minimum allowable value for XXX is 8. Then,  reboot your
  57.   computer so that the FILES=XXX parameter takes hold.
  58.  
  59.   Running  the sample program at the bottom of this file will  tell
  60.   you the maximum number of files you can open at once. (Usually 96
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  TURBO C                               NUMBER : 418
  75.   VERSION  :  1.0/1.5
  76.        OS  :  MS-DOS
  77.      DATE  :  February 23, 1988                     PAGE  : 1/14
  78.  
  79.     TITLE  :  EXTENDED FILE I/O LIBRARY
  80.  
  81.  
  82.  
  83.  
  84.   or  252,  unless  a resident program has opened  some  files  and
  85.   they have not been closed).
  86.  
  87.   THE TECHNICAL DETAILS:
  88.  
  89.   Much  of the following information is not documented in  the  DOS
  90.   Technical Reference manual.
  91.  
  92.   Under  DOS 1.0 and 1.1,  all files were accessed via File Control
  93.   Blocks (FCB's).  There was no limit to the number of FCB's that a
  94.   program could use,  so there was no limit to the number of  files
  95.   open simultaneously.
  96.  
  97.   Under  DOS 2.0 and greater,  an alternate (and preferable) method
  98.   of  accessing  files was introduced which uses a  2-byte  integer
  99.   called  a  "handle"  to refer to a  file.   A  "handle"  file  is
  100.   described  using  a data structure called a Device Control  Block
  101.   (DCB).   However,  DOS provides the storage space for all  DCB's,
  102.   rather  than having the application program store the  DCB's,  so
  103.   the  number of available DCB's is limited to the amount of  space
  104.   that  DOS  has  set  aside  for  them.   The  maximum  number  of
  105.   files/devices  that can be open simultaneously is the  number  of
  106.   slots  available in the DCB Table created by DOS.   The DCB's  in
  107.   the DCB Table are consecutively numbered, starting with 0.  DCB's
  108.   0,  1, and 2 are predefined by DOS to correspond to the AUX, CON,
  109.   and  PRN devices,  respectively.   All remaining DCB's in the DCB
  110.   Table  are  available for files or devices  used  by  application
  111.   programs.
  112.  
  113.   So that I/O redirection can be supported, the DCB numbers are not
  114.   used directly when accessing files.   Instead, a file "handle" is
  115.   used.   A  "handle" is an index into a 20-byte array,  called the
  116.   Handle  Table,  which  is  located at offset 18H of  the  Program
  117.   Segment  Prefix (PSP) for a program (for a general discussion  of
  118.   the PSP,  see the DOS Technical Reference manual).   Each element
  119.   of the Handle Table is the DCB number of a file or  device.   The
  120.   value  at index "handle" in the Handle Table is the DCB number of
  121.   the file or device that implements that file  handle.   Thus,  if
  122.   the  value 8 is in the 6th byte of the Handle Table,  the  handle
  123.   "6" refers to the file (or device) described by the DCB in slot 8
  124.   of the DCB Table.   If a handle is not currently being used,  its
  125.   entry  in  the Handle Table is FFH.   DOS predefines the first  5
  126.   handles to be primary input,  primary output,  error,  auxiliary,
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  TURBO C                               NUMBER : 418
  141.   VERSION  :  1.0/1.5
  142.        OS  :  MS-DOS
  143.      DATE  :  February 23, 1988                     PAGE  : 1/14
  144.  
  145.     TITLE  :  EXTENDED FILE I/O LIBRARY
  146.  
  147.  
  148.  
  149.  
  150.   and printer, so the first 5 entries in the Handle Table are 1, 1,
  151.   1,  0,  and 2,  corresponding to the DCB numbers for the CON (1),
  152.   AUX  (0),  and PRN (2) devices.   This leaves only  15  available
  153.   handles for opening files (or new devices).
  154.  
  155.   Every time a new handle file is opened,  a new handle gets  used.
  156.   Since there are only 20 slots available in the Handle Table for a
  157.   program, DOS only allows a "process" to have a maximum of 20 file
  158.   handles  in  use  simultaneously  (and the first  5  entries  are
  159.   predefined,  as just noted,  unless those handles get closed  and
  160.   reused).  Every new handle file requires a unique handle, so only
  161.   20 files/devices can be open at the same time by a single process
  162.   (unless  FCB's  are used).   (A "process" is any program  spawned
  163.   using  the DOS EXEC function call.   A process can be invoked  by
  164.   COMMAND.COM, or by another program.)  There can be many more than
  165.   20 DCB's in the DCB Table,  so the real limitation is in the size
  166.   of the Handle Table in the PSP.
  167.  
  168.   The  size  of  the  DCB  Table  (i.e.,   the  maximum  number  of
  169.   files/devices  that  can  be open  simultaneously  in  the  whole
  170.   computer)  is controlled by the FILES=XXX entry in the CONFIG.SYS
  171.   file.   The minimum number of slots is 8.  Under DOS 2.0/2.1, the
  172.   maximum number is 99,  and under DOS 3.0 and higher,  the maximum
  173.   is 255.   As previously mentioned,  the first three of these  DCB
  174.   slots are occupied by the AUX, CON, and PRN devices.
  175.  
  176.   A  single  program  can  use all of the DCB's in  the  DCB  Table
  177.   (except for the 3 reserved by DOS) all on its own, by effectively
  178.   bypassing  the  Handle Table in the PSP,  except on  a  temporary
  179.   basis.   The program can accomplish this feat by using, say, only
  180.   one entry in the Handle Table for all of its files.   Instead  of
  181.   allowing  DOS to store the DCB numbers in the Handle  Table,  the
  182.   program can store these numbers elsewhere.  Then, to manipulate a
  183.   file using DOS, the program can temporarily put the DCB number of
  184.   that  file into a designated slot in the Handle Table,  pass  the
  185.   index  of that table slot (i.e.,  that "handle") to DOS,  and DOS
  186.   will operate on that handle/DCB number.  In this way, DOS  can be
  187.   fooled into accessing up   to 96 (or 252) different files/devices
  188.   using a single  handle entry in the Handle Table.
  189.  
  190.   To obtain the address of the Handle Table, which is at offset 18H
  191.   in  the PSP,  the program needs to find the address of  its  PSP.
  192.   To do this we use  the  DOS  function  call  62H,    "Get Program
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  TURBO C                               NUMBER : 418
  207.   VERSION  :  1.0/1.5
  208.        OS  :  MS-DOS
  209.      DATE  :  February 23, 1988                     PAGE  : 1/14
  210.  
  211.     TITLE  :  EXTENDED FILE I/O LIBRARY
  212.  
  213.  
  214.  
  215.  
  216.   Segment Prefix  Addr